﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using D = System.Drawing;
using Microsoft.Practices.Prism.Regions;
using TimeSplicerModule.DomainModels;
using TimeSplicerModule.ViewModels;
using TimeSplicerModule.Factories;
using Microsoft.Practices.ServiceLocation;
using Microsoft.Practices.Unity;
using TimeSplicerModule.Controllers;
using TimeSplicerModule.Services;
using Infrastucture;

namespace TimeSplicerModule.Views
{
    /// <summary>
    /// Interaction logic for TaskView.xaml
    /// </summary>
    public partial class TaskView : UserControl, INavigationAware
    {
        private TaskViewModel Model;
        private readonly IUnityContainer _container;
        private readonly ITaskController _taskController;
        private readonly IProjectController _projectController;
        private readonly ITaskService _taskService;

        public TaskView(IUnityContainer container, ITaskController taskController, IProjectController projectController, ITaskService taskService)
        {
            InitializeComponent();

            _container = container;
            _taskController = taskController;
            _projectController = projectController;
            _taskService = taskService;
        }

        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return false; //throw new NotImplementedException();
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
            //throw new NotImplementedException();
        }

        public async void OnNavigatedTo(NavigationContext navigationContext)
        {
            this.progressBorder.Visibility = System.Windows.Visibility.Visible;
            this.progressRing.IsActive = true;

            if (navigationContext.Parameters.Count(x => x.Key == "parentid") == 1)
            {
                string parentid = navigationContext.Parameters["parentid"].ToString();
                Guid result;

                if (Guid.TryParse(parentid, out result))
                {
                    //Model = _taskService.CreateNewViewModel(result);
                    await Task.Run(() => Model = _taskService.CreateNewViewModel(result));


                    D.Color c = D.Color.FromName(Model.Status.StatusColor);
                    Color co = Color.FromArgb(c.A, c.R, c.G, c.B);
                    Model.Status.BrushColor = new SolidColorBrush(co);

                    this.progressBorder.Visibility = System.Windows.Visibility.Hidden;
                    this.progressRing.IsActive = false;

                    this.DataContext = Model;
                }
            }
            else if (navigationContext.Parameters.Count(x => x.Key == "id") == 1)
            {
                // asume id is passed
                string id = navigationContext.Parameters["id"].ToString();
                Guid result;

                if (Guid.TryParse(id, out result))
                {
                    //Model = _taskService.Get(result);
                    await Task.Run(() => Model = _taskService.Get(result));


                    D.Color c = D.Color.FromName(Model.Status.StatusColor);
                    Color co = Color.FromArgb(c.A, c.R, c.G, c.B);
                    Model.Status.BrushColor = new SolidColorBrush(co);

                    this.progressBorder.Visibility = System.Windows.Visibility.Hidden;
                    this.progressRing.IsActive = false;

                    this.DataContext = Model;
                }
            }
        }

        private void cancelButton_Click(object sender, RoutedEventArgs e)
        {
            if (Model != null && Model.ProjectGuid != Guid.Empty)
                _projectController.NavigateToItemView(Model.ProjectGuid);
        }

        private async void saveExitButton_Click(object sender, RoutedEventArgs e)
        {

            await Task.Run(() => _taskService.Save(Model));
            cancelButton_Click(sender, e);
        }

        private async void saveButton_Click(object sender, RoutedEventArgs e)
        {
            this.saveButton.IsEnabled = false;

            await Task.Run(() => Model = _taskService.Save(Model));

            D.Color c = D.Color.FromName(Model.Status.StatusColor);
            Color co = Color.FromArgb(c.A, c.R, c.G, c.B);
            Model.Status.BrushColor = new SolidColorBrush(co);

            this.DataContext = Model;


            this.saveButton.IsEnabled = true;
        }

        private async void activateButton_Click(object sender, RoutedEventArgs e)
        {
            MessageBoxResult result = MessageBox.Show("Are you sure you want to Activate this task?", "Confirmation", MessageBoxButton.YesNo);

            if (result == MessageBoxResult.Yes)
            {
                this.saveButton.IsEnabled = false;
                this.activateButton.IsEnabled = false;
                this.suspendButton.IsEnabled = false;
                this.completeButton.IsEnabled = false;

                await Task.Run(() => Model = _taskService.Activate(Model));

                D.Color c = D.Color.FromName(Model.Status.StatusColor);
                Color co = Color.FromArgb(c.A, c.R, c.G, c.B);
                Model.Status.BrushColor = new SolidColorBrush(co);

                this.DataContext = Model;

                this.saveButton.IsEnabled = true;
                this.activateButton.IsEnabled = Model.ActivateButtonEnabled;
                this.suspendButton.IsEnabled = Model.SuspendButtonEnabled;
                this.completeButton.IsEnabled = Model.CompletedButtonEnabled;
            }
        }

        // NOTE : Will have to handle the button enabling here

        private async void suspendButton_Click(object sender, RoutedEventArgs e)
        {
            MessageBoxResult result = MessageBox.Show("Are you sure you want to Suspend this task?", "Confirmation", MessageBoxButton.YesNo);


            if (result == MessageBoxResult.Yes)
            {
                this.saveButton.IsEnabled = false;
                this.activateButton.IsEnabled = false;
                this.suspendButton.IsEnabled = false;
                this.completeButton.IsEnabled = false;

                await Task.Run(() => Model = _taskService.Suspend(Model));

                D.Color c = D.Color.FromName(Model.Status.StatusColor);
                Color co = Color.FromArgb(c.A, c.R, c.G, c.B);
                Model.Status.BrushColor = new SolidColorBrush(co);

                this.DataContext = Model;

                this.saveButton.IsEnabled = true;
                this.activateButton.IsEnabled = Model.ActivateButtonEnabled;
                this.suspendButton.IsEnabled = Model.SuspendButtonEnabled;
                this.completeButton.IsEnabled = Model.CompletedButtonEnabled;
            }
        }

        private async void completeButton_Click(object sender, RoutedEventArgs e)
        {
            MessageBoxResult result = MessageBox.Show("Are you sure you want to Complete this task?", "Confirmation", MessageBoxButton.YesNo);

            if (result == MessageBoxResult.Yes)
            {
                this.saveButton.IsEnabled = false;
                this.activateButton.IsEnabled = false;
                this.suspendButton.IsEnabled = false;
                this.completeButton.IsEnabled = false;

                await Task.Run(() => Model = _taskService.Complete(Model));

                D.Color c = D.Color.FromName(Model.Status.StatusColor);
                Color co = Color.FromArgb(c.A, c.R, c.G, c.B);
                Model.Status.BrushColor = new SolidColorBrush(co);

                this.DataContext = Model;


                this.saveButton.IsEnabled = true;
                this.activateButton.IsEnabled = Model.ActivateButtonEnabled;
                this.suspendButton.IsEnabled = Model.SuspendButtonEnabled;
                this.completeButton.IsEnabled = Model.CompletedButtonEnabled;
            }
        }

        private void changeButtonEnabledState(bool enabled)
        {
            this.saveButton.IsEnabled = enabled;
            this.saveExitButton.IsEnabled = enabled;
            this.cancelButton.IsEnabled = enabled;
            this.activateButton.IsEnabled = enabled;
            this.suspendButton.IsEnabled = enabled;
            this.completeButton.IsEnabled = enabled;
        }
    }
}
